package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.GoodsListEntity3;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.fragment.self.GoodsStateFragment3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsStateModel3 extends BaseViewModel {
    public ObservableField<String> status = new ObservableField<>("");
    private GoodsStateFragment3 fragment;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;

    public GoodsStateModel3(GoodsStateFragment3 fragment, SelfRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //获取商品列表
    public void getGoodsListModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID,"");
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("pageNum", page);
        map.put("pageSize", size);
        map.put("text", "");
        map.put("status", status.get());
        map.put("flag", "0");
        map.put("companyId", companyId);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().myGoodsList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<GoodsListEntity3>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<GoodsListEntity3>>> data) {
                fragment.showList(data.getData().getList(), isLoadMore);
            }
        });
    }

    //上下架删除商品
    public void upDowngetGoodsModel(String opt, List<String> goodsIds) {
        Map map = new HashMap();
        map.put("status", opt);
        map.put("goodsIds", goodsIds);
        NewAPIManager.getInstance().getGoodsReleasedAPI().upDown(map).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                fragment.showOptSuccess(opt, goodsIds.size());
            }
        });
    }

    //商品改价
    public void modifyGoodsPriceModel(String price, String type,List<String> goodsIds) {
        Map map = new HashMap();
        map.put("price", price);
        map.put("type", type);
        map.put("goodsIds", goodsIds);
        NewAPIManager.getInstance().getGoodsReleasedAPI().modifyGoodsPrice(map).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                getGoodsListModel(false);
            }
        });
    }
}
